<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

try {
  $hostname = HOST_NAME;
  $username = USER_NAME2;
  $password = PASSWORD2;
  $dbname = DATABASE_NAME2;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}


//$orm1 = classToOrm('produceselect');
try {
  $stmt = $myPdo->prepare("SELECT p.e_oid AS id, 
                                  iu.estimates AS estimates, iu.extras AS extras, iu.travels AS travels, 
                                  im.createdDate AS createdDate, im.sentDate AS sentDate, im.receivedDate AS receivedDate, im.receiver AS receiver, im.address AS address, im.code AS code, im.company AS company, im.weight AS weight, im.comment AS comment, im.review AS review, 
                                  u.trackId AS user_trackId, u.account AS user_account, u.name AS user_name, u.email AS user_email, u.type AS user_type, 
                                  i.trackId AS pi_trackId, i.sampleId AS pi_sampleId, i.name AS pi_name, 
                                  i.price AS pi_price, i.unit AS pi_unit, i.type AS pi_type, 
                                  i.model AS pi_model, i.otherId AS pi_otherId, i.quantity AS pi_quantity, 
                                  i.total AS pi_total, i.packageInc AS pi_packageInc 
                           FROM ns_Produce AS p LEFT JOIN ns__ez_relation_produce_produceitemuser AS piu ON p.e_oid = piu.oid_a
                                                LEFT JOIN ns_ProduceItemUser AS iu ON iu.e_oid = piu.oid_b
                                                LEFT JOIN ns__ez_relation_produce_produceitemmail AS pim ON p.e_oid = pim.oid_a
                                                LEFT JOIN ns_ProduceItemMail AS im ON im.e_oid = pim.oid_b
                                                LEFT JOIN ns__ez_relation_produce_produceuser AS pu ON (p.e_oid = pu.oid_a AND pu.var_a = 'who') 
                                                LEFT JOIN ns_ProduceUser AS u ON u.e_oid = pu.oid_b
                                                LEFT JOIN ns__ez_relation_produce_producepurchaseitem AS pi ON (p.e_oid = pi.oid_a AND pi.var_a = 'purchaseitem') 
                                                LEFT JOIN ns_ProducePurchaseItem AS i ON i.e_oid = pi.oid_b
                          ");
  $stmt->execute();
  
  //$myManager->start_t();
  $myPdo->beginTransaction();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $produce = $stmt->fetch(PDO::FETCH_OBJ);
    
    $sql = "";
    if (isset($produce->estimates) &&
        isset($produce->extras) &&
        isset($produce->travels)) {
      $sql .= (empty($sql) ? "" : ", ") . "estimates = '" . $produce->estimates . "'";
      $sql .= (empty($sql) ? "" : ", ") . "extras = '" . $produce->extras . "'";
      $sql .= (empty($sql) ? "" : ", ") . "travels = '" . $produce->travels . "'";
    }
    if (isset($produce->createdDate) &&
        isset($produce->sentDate) &&
        isset($produce->receivedDate) &&
        isset($produce->receiver) &&
        isset($produce->address) &&
        isset($produce->code) &&
        isset($produce->company) &&
        isset($produce->weight) &&
        isset($produce->comment) &&
        isset($produce->review)) {
      $sql .= (empty($sql) ? "" : ", ") . "createdDate = '" . $produce->createdDate . "'";
      $sql .= (empty($sql) ? "" : ", ") . "sentDate = '" . $produce->sentDate . "'";
      $sql .= (empty($sql) ? "" : ", ") . "receivedDate = '" . $produce->receivedDate . "'";
      $sql .= (empty($sql) ? "" : ", ") . "receiver = '" . $produce->receiver . "'";
      $sql .= (empty($sql) ? "" : ", ") . "address = '" . $produce->address . "'";
      $sql .= (empty($sql) ? "" : ", ") . "code = '" . $produce->code . "'";
      $sql .= (empty($sql) ? "" : ", ") . "company = '" . $produce->company . "'";
      $sql .= (empty($sql) ? "" : ", ") . "weight = '" . $produce->weight . "'";
      $sql .= (empty($sql) ? "" : ", ") . "comment = '" . $produce->comment . "'";
      $sql .= (empty($sql) ? "" : ", ") . "review = '" . $produce->review . "'";
    }
    if (isset($produce->user_trackId) &&
        isset($produce->user_account) &&
        isset($produce->user_name) &&
        isset($produce->user_email) &&
        isset($produce->user_type)) {
      $sql .= (empty($sql) ? "" : ", ") . "user_trackId = '" . $produce->user_trackId . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_account = '" . $produce->user_account . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_name = '" . $produce->user_name . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_email = '" . $produce->user_email . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_type = '" . $produce->user_type . "'";
    }
    if (isset($produce->pi_trackId) &&
        isset($produce->pi_sampleId) &&
        isset($produce->pi_name) &&
        isset($produce->pi_price) &&
        isset($produce->pi_unit) &&
        isset($produce->pi_type) &&
        isset($produce->pi_model) &&
        isset($produce->pi_otherId) &&
        isset($produce->pi_quantity) &&
        isset($produce->pi_total) &&
        isset($produce->pi_packageInc)) {
      $sql .= (empty($sql) ? "" : ", ") . "pi_trackId = '" . $produce->pi_trackId . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_sampleId = '" . $produce->pi_sampleId . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_name = '" . $produce->pi_name . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_price = '" . $produce->pi_price . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_unit = '" . $produce->pi_unit . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_type = '" . $produce->pi_type . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_model = '" . $produce->pi_model . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_otherId = '" . $produce->pi_otherId . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_quantity = '" . $produce->pi_quantity . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_total = '" . $produce->pi_total . "'";
      $sql .= (empty($sql) ? "" : ", ") . "pi_packageInc = '" . $produce->pi_packageInc . "'";
      /**
      $selects = array();
      if (isset($produce->user__selects)) {
        foreach($produce->purchaseItem->selects as &$select) {
          $sel = new stdClass();
          $sel->trackId = $select->trackId;
          $sel->produce_trackId = $produce->trackId;
          $sel->title = $select->title;
          $sel->value = $select->value;
          array_push($selects, $sel);
        }
      }*/
    }
    /**
    foreach($selects as &$s) {
      $orm1->add($myManager, $s);
    }*/
    if (!empty($sql)) {
      $myPdo->exec("UPDATE ns_Produce SET $sql WHERE e_oid = $produce->id");
    }
    $i++;
  }
  $myPdo->commit();
  //$myManager->commit_t();
    
  echo json_encode(array());
} catch (PDOException $e) {
  $myPdo->rollBack();
  echo $e->getMessage();
}
?>